package com.suse.service.view.order.controller;

import cn.hutool.core.util.ObjectUtil;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.common.util.jwt.JwtUtil;
import com.suse.model.dto.UserDto;
import com.suse.service.view.order.entity.Order;
import com.suse.service.view.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-20
 */
@Api(tags = "订单接口")
@RestController
// 不需要 /order
@RequestMapping
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation(value = "创建订单")
    @PostMapping("/auth/save/{courseId}")
    public R saveOrder(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto userDto = JwtUtil.parseToken(token, UserDto.class);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未登录或登录已过期，请重新登录！");
        }
        String userId = userDto.getId();
        return orderService.saveOrder(courseId,userId);
    }

    @ApiOperation(value = "通过ID获取订单信息")
    @GetMapping("/auth/info/{id}")
    public R getOrderInfoById(@PathVariable("id") String id, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto userDto = JwtUtil.parseToken(token, UserDto.class);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未登录或登录已过期，请重新登录！");
        }
        Order order = orderService.getOrderInfoById(id,userDto.getId());
        return R.success(order);
    }

    @ApiOperation(value = "判断课程是否已购买")
    @GetMapping("/auth/hasBuy/{courseId}")
    public R hasBuy(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto userDto = JwtUtil.parseToken(token, UserDto.class);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未登录或登录已过期，请重新登录！");
        }
        String userId = userDto.getId();
        boolean hasBuy = orderService.hasBuy(courseId,userId);
        return R.put("hasBuy",hasBuy).success();
    }

    @ApiOperation(value = "获取我的订单列表")
    @GetMapping("/auth/list")
    public R getOrderList(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto userDto = JwtUtil.parseToken(token, UserDto.class);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未登录或登录已过期，请重新登录！");
        }
        String userId = userDto.getId();
        return orderService.getOrderList(userId);
    }

    @ApiOperation(value = "删除我的订单")
    @DeleteMapping("/auth/remove/{orderId}")
    public R removeMyOrder(@PathVariable("orderId") String orderId,HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto userDto = JwtUtil.parseToken(token, UserDto.class);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未登录或登录已过期，请重新登录！");
        }
        String userId = userDto.getId();
        return orderService.removeMyOrder(orderId,userId);
    }

}

